<?php

namespace app\controller\tools;

use think\facade\Config;
use think\facade\Db;


class Where extends Base
{
	
	// where操作
	
	/**
	 * 1、字符串转数组
	 */
	
	// 3.2的测试
	public function sql()
	{
		// SELECT * FROM `yi_admin` WHERE `id` = 1 LIMIT 1
		// $sql = Db::name('admin')->fetchSql(true)->find(1);
		
		// WHERE OR
		// SELECT * FROM `yi_admin` WHERE ( `username` LIKE '%haha%' OR `password` LIKE '%haha%' ) AND `id` = 1 LIMIT 1
		$sql = Db::name('admin_user')->where('username|password','like','%haha%')->fetchSql(true)->find(1);
		
		// WHERE AND
		// SELECT * FROM `yi_admin` WHERE `password` LIKE '%haha%' AND `username` LIKE '%haha%' AND `id` = 1 LIMIT 1
		// $where = [['password', 'like', '%haha%'], ['username', 'like', '%haha%']];
		// $sql   = Db::name('admin')->where($where)->fetchSql(true)->find(1);
		
		// 错误：OR 的两个条件没有括号 与 AND 组合不对
		// WHERE OR
		// SELECT * FROM `yi_admin` WHERE `password` LIKE '%haha%' OR `username` LIKE '%haha%' AND `id` = 1 LIMIT 1
		//$where = [['password', 'like', '%haha%'], ['username', 'like', '%haha%']];
		//$sql   = Db::name('admin')->whereOr($where)->fetchSql(true)->find(1);
		
		
		// 错误：OR 的两个条件没有括号 与 AND 组合不对
		// SELECT * FROM `yi_admin` WHERE `username` = '%haha%' OR `phone` = '%haha%' OR `email` = '%haha%' AND `id` = 1 LIMIT 1
		// $whereOr = ["username" => '%haha%', "phone" => '%haha%', "email" => '%haha%'];
		// $sql   = Db::name('admin')->whereOr($whereOr)->fetchSql(true)->find(1);
		
		// 报错
		// 在上面的条件上在加一个外层数组
		// $whereOr = [["username" => '%haha%', "phone" => '%haha%', "email" => '%haha%']];
		// $sql   = Db::name('admin')->whereOr($whereOr)->fetchSql(true)->find(1);
		
		// TODO 多个 OR 只能将 AND 组合在多个OR的内部使用
		// SELECT * FROM `yi_admin` WHERE ( `username` = '%haha%' ) OR ( `phone` = '%haha%' ) OR ( `email` = '%haha%' ) AND `id` = 1 LIMIT 1
		// $where = [[["username" ,'=', '%haha%']], [["phone" ,'=', '%haha%']], [["email" ,'=', '%haha%']]];
		// $sql   = Db::name('admin')->whereOr($where)->fetchSql(true)->find(1);
		
		// TODO 多个 OR 只能将 AND 组合在多个OR的内部使用
		// 错误：OR 的两个条件没有括号 与 AND 组合不对
		// WHERE OR
		// SELECT * FROM `yi_admin` WHERE ( `password` LIKE '%haha%' ) OR ( `username` LIKE '%haha%' ) AND `id` = 1 LIMIT 1
		// $map1  = [['password', 'like', '%haha%']];
		// $map2  = [['username', 'like', '%haha%']];
		// $where = [$map1, $map2];
		// $sql   = Db::name('admin')->whereOr($where)->fetchSql(true)->find(1);
		
		
		echo $sql;
	}
	
	//  1、字符串转数组
	public function toArray()
	{
		$where  = "";
		$where  = "'status=' . Config::get('xiaohua.JOKE_STATUS_AUDIT') . ' and type=' . Config::get('xiaohua.TYPE_TEXT_PIC') . ' and commend=1'";
		$where  = str_replace(' ', '', $where);
		$wheres = explode('and', $where);
		var_dump($wheres);
		echo '<br>';
		echo '<br>';
		$array = [];
		foreach($wheres as $_item){
			$_items = explode('=', $_item);
			var_dump($_items);
			echo '<br>';
		}
	}
	
	// 数组整理
	public function makeArray()
	{
		$data                 = array();
		$data['type']         = '';
		$data['feed_id']      = '$id';
		$data['user_id']      = '$this->user[\'id\']';
		$data['package']      = 1;
		$data['created_time'] = THINK_TIME;
		
		var_export($data);
	}
	
	
}
